package org.softmed.rest.server.config;

import java.io.File;
import java.util.StringTokenizer;

import org.softmed.filehandling.FileUtil;
import com.thoughtworks.xstream.XStream;

public class ConfigReader {

	String path = "app-config.xml";

	String uriPath = "root-uri.txt";

	public Config read() throws Throwable {

		FileUtil util = new FileUtil();
		File file = util.getFile(path);
		System.out.println("Trying to read configuration from "
				+ file.getCanonicalPath());
		String xml = util.readFromFile(file);

		// System.out.println("CONFIG XML: " + xml);

		XStream stream = new XStream();
		stream.alias("config", Config.class);
		// stream.omitField(Config.class, "domain");
		stream.aliasField("deploy-admin", Config.class, "deployAdmin");
		Config cfg = (Config) stream.fromXML(xml);

		// try to read a root uri file
		File root = util.getFile(uriPath);
		if (!root.exists())
			return cfg;

		String rootUri = util.readFromFile(root);
		StringTokenizer st = new StringTokenizer(rootUri);
		if (st.hasMoreTokens())
			cfg.domain = st.nextToken();

		return cfg;
	}

}
